import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import "@/assets/styles/base.css"
import "lib-flexible"
import axios from "axios";
// axios.defaults.timeout = 10000;
import { Toast } from 'vant';
import 'vant/lib/index.css'
import 'vant/lib/index.js'
// import createTime from "@/api/formatTime.js";

Vue.use(Toast);
Vue.filter("timeago", function (time) {
  let data = new Date(time);
  let dateTimeStamp = data.getTime();
  let min = 1000 * 60;
  let hour = min * 60;
  let day = hour * 24;
  let week = day * 7;
  let month = day * 30;
  let year = month * 12;
  let now = new Date().getTime();
  let diffValue = now - dateTimeStamp;

  let result = "";
  if (diffValue < 0) {
    result = "" + "未来";
  }
  let minC = diffValue / min;
  let hourC = diffValue / hour;
  let dayC = diffValue / day;
  let weekC = diffValue / week;
  let monthC = diffValue / month;
  let yearC = diffValue / year;

  if (yearC >= 1) {
    result = "" + parseInt(yearC) + "年前";
  } else if (monthC >= 1 && monthC < 12) {
    result = "" + parseInt(monthC) + " 个月前";
  } else if (weekC >= 1 && weekC < 5 && dayC > 6 && monthC < 1) {
    result = "" + parseInt(weekC) + "周前";
  } else if (dayC >= 1 && dayC <= 6) {
    result = "" + parseInt(dayC) + "天前";
  } else if (hourC >= 1 && hourC <= 24) {
    result = "" + parseInt(hourC) + "小时前";
  } else if (minC >= 1 && minC <= 59) {
    result = "" + parseInt(minC) + "分钟前";
  } else if (diffValue >= 0 && diffValue <= min) {
    result = "刚刚";
  }
  return result;
});
axios.interceptors.request.use(
  function (config) {
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
      duration: 0,
    });
    let token = localStorage.getItem('h5-token');
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  function (error) {
    Toast.clear();
    return Promise.reject(error);
  }
);
// 响应拦截器
axios.interceptors.response.use(
  function (response) {
    Toast.clear();
    // 才是后台真正返回的数据
    // 请求没有正确数据返回
    // console.log(response.data.code);
    if (response.data.code != 0) {
      Toast.fail(response.data.message);
    }

    return response.data;
  },
  function (error) {
    Toast.clear();
    return Promise.reject(error);
  }
);
Vue.prototype.$axios = axios;
Vue.config.productionTip = false;
Vue.prototype.$myCreateTime = function (time) {
  let data = new Date(time);
  let dateTimeStamp = data.getTime();
  let min = 1000 * 60;
  let hour = min * 60;
  let day = hour * 24;
  let week = day * 7;
  let month = day * 30;
  let year = month * 12;
  let now = new Date().getTime();
  let diffValue = now - dateTimeStamp;

  let result = "";
  if (diffValue < 0) {
    result = "" + "未来";
  }
  let minC = diffValue / min;
  let hourC = diffValue / hour;
  let dayC = diffValue / day;
  let weekC = diffValue / week;
  let monthC = diffValue / month;
  let yearC = diffValue / year;

  if (yearC >= 1) {
    result = "" + parseInt(yearC) + "年前";
  } else if (monthC >= 1 && monthC < 12) {
    result = "" + parseInt(monthC) + " 个月前";
  } else if (weekC >= 1 && weekC < 5 && dayC > 6 && monthC < 1) {
    result = "" + parseInt(weekC) + "周前";
  } else if (dayC >= 1 && dayC <= 6) {
    result = "" + parseInt(dayC) + "天前";
  } else if (hourC >= 1 && hourC <= 24) {
    result = "" + parseInt(hourC) + "小时前";
  } else if (minC >= 1 && minC <= 59) {
    result = "" + parseInt(minC) + "分钟前";
  } else if (diffValue >= 0 && diffValue <= min) {
    result = "刚刚";
  }
  return result;
}
// import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css';
router.beforeEach((to, from, next) => {
  // console.log('进入：', to);
  // console.log('从：', from);
  // next();
  // next();
  // next(false);
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  // console.log('router.path',from.path );
  //   console.log('router.path ',to.path);
  //   console.log('router.path ',window.location.href);
  if (to.meta.auth) {
    // 说明你要权限才能访问
    let token = localStorage.getItem('h5-token');
    // if(router.path == next.path){
    //   console.log(router.path);
    // }
    
    if (token) {

      next();
    } else {
      // next({

      //   path: '/login',
      //   // query: {
      //   //   url: window.location.href,
      //   // },
      // });
      router.push({
        path: "/login",
      });
    }
  } else {
    next();
  }
});

// Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');
